package ir;

import java.util.ArrayList;
import java.util.List;

import run.Bind;
import run.Facet;

/**
 * Bind Types (binding operators execute, linking facets)
 * @author Thomas Chau
 *
 */
public class BindType {
	private String bindType;

	public BindType(String bindType) {
		this.bindType = bindType;
	}

	public List<Bind> bind(List<Facet> leftBindables, List<Facet> rightBindables) throws Exception {
		List<Bind> bindings = new ArrayList<Bind>();
		
		if (rightBindables.size() < 1) {
			throw new Exception("BINDTYPE::FAILED: BINDING COULD NOT FIND RIGHT BINDABLES. Please specify the facets.");
		}
		if (leftBindables.size() < 1) {
			throw new Exception("BINDTYPE::FAILED: BINDING COULD NOT FIND LEFT BINDABLES. Please specify the facets. SPECIFICALLY.");
		}
		
		if (bindType.equals("->")) {
			
			for (Facet left : leftBindables)
				for (Facet right : rightBindables) 
					bindings.add(new Bind(left, this, right));
			
		}
		else if (bindType.equals("=>")) {
			int i = 0;
			for (Facet left : leftBindables) {
				Facet right = rightBindables.get(i);
				bindings.add(new Bind(left, this, right));
				i++;
			}
			
		}
		else {
			System.out.println("Bind failed: incorrect or unsupported bind type.");
			(new Exception()).printStackTrace();
		}
		
		return bindings;
	}
	
	public String toString() {
		return bindType;
	}
}
